package com.ruoyi.web.controller.orderManage;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.CheckinMessage;
import com.ruoyi.system.domain.GuestRoom;
import com.ruoyi.system.domain.OrderManage;
import com.ruoyi.system.domain.Unsubscribe;
import com.ruoyi.system.service.CheckinMessageService;
import com.ruoyi.system.service.GuestRoomService;
import com.ruoyi.system.service.UnsubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;


@RestController
@RequestMapping("/system/checkin")
public class CheckinMessageController extends BaseController {


    @Autowired
    private CheckinMessageService checkinMessageService;
    @Autowired
    private UnsubscribeService unsubscribeService;


    @PreAuthorize("@ss.hasPermi('system:checkin:list')")
    @GetMapping("/list")
    public TableDataInfo list(CheckinMessage checkinMessage)
    {
        startPage();
        List<CheckinMessage> list = checkinMessageService.selectCheckinMessageList(checkinMessage);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:checkin:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Map<String,Object> map)
    {
        CheckinMessage checkinMessage = new CheckinMessage();
        // 15位数字的最小值是100000000000000L
        long min = 100000000000000L;
        // 15位数字的最大值是999999999999999L
        long max = 999999999999999L;

        Random random = new Random();
        // 生成15位随机数
        long random15Digits = min + ((long)(random.nextDouble() * (max - min + 1)));
        checkinMessage.setNumberId(random15Digits+"");

        Instant instant = Instant.parse((String) map.get("checkinTime"));
        Date date = Date.from(instant);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, Integer.parseInt((String) map.get("inDays"))); // 增加1天
        Date nextDay = calendar.getTime();
        checkinMessage.setCheckinTime(date);
        checkinMessage.setCheckinDate(nextDay);

        checkinMessage.setRoomId((String) map.get("roomId"));
        checkinMessage.setCheckinName((String) map.get("checkinName"));
        checkinMessage.setCheckinDeposit((Integer) map.get("checkinDeposit"));
        checkinMessage.setCheckinRoom((String) map.get("checkinRoom"));
        checkinMessage.setCheckinType((String) map.get("checkinType"));
        checkinMessage.setCheckinPrice((Integer) map.get("checkinPrice"));
        checkinMessage.setInDays((String) map.get("inDays"));
        checkinMessage.setCheckinPhone((String) map.get("checkinPhone"));
        return toAjax(checkinMessageService.insertCheckinMessage(checkinMessage));
    }

    @PreAuthorize("@ss.hasPermi('system:checkin:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CheckinMessage checkinMessage)
    {
        Date checkinTime = checkinMessage.getCheckinTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(checkinTime);
        calendar.add(Calendar.DATE, Integer.parseInt((String) checkinMessage.getInDays())); // 增加1天
        Date nextDay = calendar.getTime();
        checkinMessage.setCheckinDate(nextDay);
        return toAjax(checkinMessageService.updateCheckinMessage(checkinMessage));
    }

    @PreAuthorize("@ss.hasPermi('system:checkin:continueLiving')")
    @PostMapping("/continueLiving")
    public AjaxResult continueLiving(@RequestBody Map<String ,Object> map)
    {
        CheckinMessage checkinMessage = new CheckinMessage();
        checkinMessage.setNumberId((String) map.get("numberId"));
        List<CheckinMessage> list = checkinMessageService.selectCheckinMessageList(checkinMessage);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(list.get(0).getCheckinDate());
        calendar.add(Calendar.DATE, Integer.parseInt((String) map.get("days"))); // 增加1天
        Date nextDay = calendar.getTime();
        checkinMessage.setCheckinDate(nextDay);
        return toAjax(checkinMessageService.updateCheckinMessageDateOut(checkinMessage));
    }

    @PreAuthorize("@ss.hasPermi('system:checkin:checkingOut')")
    @PostMapping("/checkingOut")
    public AjaxResult checkingOut(@RequestBody Map<String ,Object> map)
    {
        CheckinMessage checkinMessage = new CheckinMessage();
        checkinMessage.setNumberId((String) map.get("numberId"));
        List<CheckinMessage> list = checkinMessageService.selectCheckinMessageList(checkinMessage);

        Unsubscribe unsubscribe = new Unsubscribe();
        unsubscribe.setNumberId(list.get(0).getNumberId());
        unsubscribe.setRoomId(list.get(0).getRoomId());
        unsubscribe.setCancelName(list.get(0).getCheckinName());
        unsubscribe.setCancelReason("");
        unsubscribe.setCancelCheckOut("退房");
        unsubscribe.setCancelCost((Integer) map.get("CheckOutSettlement") == 0 ?list.get(0).getCheckinDeposit():(Integer) map.get("CheckOutSettlement"));
        unsubscribe.setCancelType(list.get(0).getCheckinType());
        unsubscribe.setRemak((String) map.get("checkOutDetails"));
        unsubscribe.setDateOut(new Date());
        unsubscribeService.insertUnsubscribe(unsubscribe);
        return toAjax(checkinMessageService.updateCheckinMessageDateOut(checkinMessage));
    }

    @PreAuthorize("@ss.hasPermi('system:checkin:delete')")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody String numberId)
    {
        return toAjax(checkinMessageService.deleteCheckinMessage(numberId));
    }
}
